#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Collections;
using WaveMaster.Holidays.Club.Domain;
using WaveMaster.Holidays.Club.Service;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class SearchTravelAgentForm : Form
    {
        TravelAgentService m_travelAgentService;
        UserControlLogin m_parent;

        public SearchTravelAgentForm(UserControlLogin parent, string text)
        {
            InitializeComponent();
            m_parent = parent;
            m_travelAgentService = new TravelAgentService();// (TravelAgentService)ServiceFactory.Instance().GetService(ServiceFactory.TRAVEL_AGENT_SERVICE);
            textBoxSearch.Text = text;
            //textBoxSearch.SelectAll();
            buttonFind_Click(null, null);
            //this.Activated += new EventHandler(SearchTravelAgentForm_Activated);
        }

        //void SearchTravelAgentForm_Activated(object sender, EventArgs e)
        //{
        //    textBoxSearch.SelectAll();
        //    listViewRecords.Items.Clear();
        //}

        private void buttonFind_Click(object sender, EventArgs e)
        {
            listViewRecords.Items.Clear();
            IList users = m_travelAgentService.SearchActiveTravelAgentByCodeName(textBoxSearch.Text);
            foreach (TravelAgent user in users)
            {
                //user.Role = m_userService.GetRoleMasterByID(user.Role.RoleMasterID);
                ListViewItem item = listViewRecords.Items.Add(user.Code);
                item.SubItems.Add(user.Name);
                item.Tag = user;
            }
            if (users.Count > 1)
            {
                listViewRecords.Focus();
                listViewRecords.SelectedIndex = 0;
            }
            else if (users.Count == 0)
            {
                ListViewItem item = listViewRecords.Items.Add("Not Found.");
                textBoxSearch.SelectAll();
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            chooseTAandClose();
        }

        private void chooseTAandClose()
        {
            if (listViewRecords.SelectedItem == null) return;
            TravelAgent ta = (TravelAgent)listViewRecords.SelectedItem.Tag;
            m_parent.LoadBook(ta);
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            chooseTAandClose();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}